package com.ltmall.api.refund.wx.json;

import com.ltmall.api.refund.wx.bean.WX;
import net.sf.json.JSON;
import net.sf.json.xml.XMLSerializer;
import org.json.JSONException;
import org.json.JSONObject;

import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;

//import net.sf.json.JSON;
//import net.sf.json.JSONException;
//import net.sf.json.JSONObject;
//import net.sf.json.xml.XMLSerializer;

public class XmlTOJson {
	private static final String STR_JSON = "{\"name\":\"Michael\",\"address\":{\"city\":\"Suzou\",\"street\":\" Changjiang Road \",\"postcode\":100025},\"blog\":\"http://www.ij2ee.com\"}";

	public static WX getWX(String xml) {
		Map<String ,String> map =  toMap(myXML(xml));
		WX wx = (WX) toJavaBean(new WX(), map);
		return wx;
	}

    /**
     * 将Json对象转换成Map
     * @param jsonObject
     *            json对象
     * @return Map对象
     * @throws JSONException
     */
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public static Map toMap(String jsonString) {
		JSONObject jsonObject;
		Map result = new HashMap();
		try {
			jsonObject = new JSONObject(jsonString);
			Iterator iterator = jsonObject.keys();
			String key = null;
			String value = null;
			while (iterator.hasNext()) {
				key = (String) iterator.next(); 
				Object obj = jsonObject.get(key);
				if(obj instanceof String){
					value = obj.toString();
					result.put(key, value);
				}
			}
		} catch (JSONException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return result;
	}
	
	public static Object toJavaBean(Object javabean, Map data) {
		Method[] methods = javabean.getClass().getDeclaredMethods();
		for (Method method : methods) {
			try {
				if (method.getName().startsWith("set")) {
					String field = method.getName();
					field = field.substring(field.indexOf("set") + 3);
					field = field.toLowerCase().charAt(0) + field.substring(1);
					method.invoke(javabean, new Object[] {
					data.get(field)
					});
				}
			} catch (Exception e) {
			}
		}
		return javabean;
	}
	
	public static String myXML(String xml){
         XMLSerializer xmlSerializer = new XMLSerializer(); 
         JSON json = xmlSerializer.read( xml );  
         
         System.out.println(json);
        return json.toString();
	}
	
//	public static String json2XML(String json) {
//		JSONObject jobj = JSONObject.fromObject(json);
//		String xml = new XMLSerializer().write(jobj);
//		return xml;
//	}	

	@SuppressWarnings("unchecked")
	public static void main(String[] args) {
		String XMLStr = "<xml><return_code><![CDATA[SUCCESS]]></return_code><return_msg><![CDATA[OK]]></return_msg><appid><![CDATA[wx46e1d4377e2b58e6]]></appid><mch_id><![CDATA[1336857501]]></mch_id><nonce_str><![CDATA[A1eeTQfCiEU6O9nI]]></nonce_str><sign><![CDATA[49A973BB99CBE8447D0D83F75A9E4546]]></sign><result_code><![CDATA[SUCCESS]]></result_code><transaction_id><![CDATA[4008322001201607108700911152]]></transaction_id><out_trade_no><![CDATA[NO201607101413182458521772]]></out_trade_no><out_refund_no><![CDATA[20160710100]]></out_refund_no><refund_id><![CDATA[2008322001201607100317481115]]></refund_id><refund_channel><![CDATA[]]></refund_channel><refund_fee>1</refund_fee><coupon_refund_fee>0</coupon_refund_fee><total_fee>1</total_fee><cash_fee>1</cash_fee><coupon_refund_count>0</coupon_refund_count><cash_refund_fee>1</cash_refund_fee></xml>";
		Map<String ,String> map =  toMap(myXML(XMLStr));
		WX wx = (WX) toJavaBean(new WX(), map);
		System.out.println(wx.getResult_code());
	}
}
